﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Controllers
{
    public class DepartmentController : BaseController
    {
        public IDepartmentService DepartmentService { get; set; }
        // GET: Department
        public string Index()
        {
            var list = DepartmentService.GetDepartmentView();
            JsonMessage msg =  JsonHandler.CreateMessage((short)MsgTypeEnum.成功, "", JsonHandler.ToJson(list));
            return JsonHandler.ToJson(msg);
        }

        #region 添加
        public string Create(Department department)
        {
            department.DelFlag = delflagNormal;
            department.CreateTime = DateTime.Now;
            department.ModifiedOn = DateTime.Now;
            department.DeptCode = DepartmentService.GetDeptCode(department.ParentId ?? 0);
            department.IsLeaf = (short)AllEnum.IsLeafEnum.是;

            DepartmentService.Add(department);
            if (department.ParentId > 0)
            {
                var parentModel = DepartmentService.GetModel(department.ParentId ?? 0);
                if (parentModel != null)
                {
                    parentModel.IsLeaf = DepartmentService.GetEntities(u => u.DelFlag == 0 && u.ParentId == department.ParentId).Count() > 0 ? (short)AllEnum.IsLeafEnum.否 : (short)AllEnum.IsLeafEnum.是;
                    DepartmentService.Update(parentModel);
                }
            }
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
            return JsonHandler.ToJson(message);
        }

        #endregion
        #region 编辑
        public string Edit(Department department)
        {
            Department editDepartment = DepartmentService.GetModel(department.Id);
            string res = string.Empty;
            if (editDepartment == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editDepartment.DeptName = department.DeptName;
                if(editDepartment.ParentId != department.ParentId)
                {
                    editDepartment.DeptCode= DepartmentService.GetDeptCode(department.ParentId ?? 0);
                }
                editDepartment.ParentId = department.ParentId;
                editDepartment.IsLeaf = DepartmentService.GetEntities(u => u.DelFlag == 0 && u.ParentId == department.Id).Count() > 0 ? (short)AllEnum.IsLeafEnum.否 : (short)AllEnum.IsLeafEnum.是;
                editDepartment.IsCheck = department.IsCheck;
                editDepartment.Sort = department.Sort;
                editDepartment.Remark = department.Remark;
                editDepartment.ModifiedOn = DateTime.Now;

                bool success = DepartmentService.Update(editDepartment);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                }
            }

            return res;
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            Department department = DepartmentService.GetModel(id);
            string res = string.Empty;
            if (department == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                department.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = DepartmentService.Update(department);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion


    }
}